package com.example.webdemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.webdemo.bean.User;
import com.example.webdemo.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Date 2021/6/22 15:56
 * @Created by wangle
 * @Description
 */
@Controller
public class Tablecontroller {

    @Autowired
    Userservice userservice;

    @GetMapping("/basic_table")
    public  String basicTable(){

        return "table/basic_table";
    }

    @GetMapping("/dynamic_table")
    public  String dynamicTable( @RequestParam(value = "pn",defaultValue ="1") Integer pn, Model model){

        //表格内容的遍历
//        List<User> users= Arrays.asList(new User("张三","12123"),new User("lisi","23142")
//        ,new User("wangwu","124231"),new User("aaa","124231"),
//        new User("qew","124231"));
//
//        model.addAttribute("users",users);
//        //从数据库中查出user表
//
        List<User> users= userservice.list();
//        model.addAttribute("users",users);

        //分页查询数据
        Page<User> userPage=new Page<User>(pn,2);
//        分页查询的结果
        Page<User> page= userservice.page(userPage,null);

        model.addAttribute("page",page);

       return "table/dynamic_table";

    }
    @GetMapping("/user/delete/{id}")
    public  String deleteUser(@PathVariable("id") Integer id,
                              @RequestParam(value = "pn",defaultValue = "1")Integer pn
                                ,RedirectAttributes ra){

        userservice.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }


    @GetMapping("/responsive_table")
    public  String responsiveTable(){

        return "table/responsive_table";
    }


    @GetMapping("/editable_table")
    public  String editableTable(){

        return "table/editable_table";
    }
    @GetMapping("/pricing_table")
    public  String pricingTable(){

        return "table/pricing_table";
    }


}
